﻿#ifndef ROBOTCOMMON
#define ROBOTCOMMON

#ifdef WIN32
#ifdef GENESDK_EXPORTS
#define GENESDK_API __declspec(dllexport)
#elif GENESDK_STATIC
#define GENESDK_API
#else
#define GENESDK_API __declspec(dllimport)
#endif
#else
#define GENESDK_API
#endif

//#pragma pack(push)
//#pragma pack(4)

typedef enum JointType
{
	TRANS_JOINT = 0,
	ROTAT_JOINT = 1
}JointType;

typedef struct JointsLimit
{
	char  name[64];
	JointType  type;
	float  leftRange;
	float  rightRange;
}JointsLimit;

typedef struct MotorSpeed
{
	char  name[64];
	float  motorSpeed;
	float  motorSpeedLimit;
}MotorSpeed;

#define   ENCODER_STATUS   0
#define  UNITLOOP_STATUS   1
#define MUNITLOOP_STATUS   2
#define     SERVO_STATUS   3
#define ERRORCODE_STATUS   4
#define        KP_STATUS   5
#define        KV_STATUS   6
#define        KI_STATUS   7

typedef struct ServoInfo
{
	long int       encoder;
	long int       unitLoop;
	long int       multiLoop;
	unsigned int   status;
	unsigned int   errorCode;
	unsigned int   KP;
	unsigned int   KV;
	unsigned int   KI;
	int            valueState[8];
}ServoInfo;

typedef enum ExecutionMode
{
	EXECUTION_MODE_CONTINOUS = 1,	// Normal, production, execution.
	EXECUTION_MODE_STEPIN,		// Stepwise execution, enter routines.
	EXECUTION_MODE_STEPOVER,		// Stepwise execution, step over routines
	EXECUTION_MODE_STEPOUT,		// Step out of current routine.
	EXECUTION_MODE_STEPBACK,		// Execute one step backwards.
	EXECUTION_MODE_STEPLAST,		// Step to last statement in current routine.
	EXECUTION_MODE_STEMPMOTION,	// Step to next motion instruction.
	EXECUTION_MODE_UNDEFINED		// Execution mode not defined (can not be set).
} HelperExecutionMode;

typedef enum ServoState
{
	SERVO_UNKNOWN = 0,
	SERVO_ERROR = 1,
	SERVO_INITED = 2,
	SERVO_ENABLED = 3,
	SERVO_DISABLED = 4
}ServoState;

typedef enum PGMState
{
	PGM_WRONG = 0,
	PGM_READY = 1,
	PGM_RUNNING = 2,
	PGM_STOPPED = 3,
	PGM_UNINITED = 4
}PGMState;

typedef enum TaskState
{
	TASK_WRONG = 0,
	TASK_EMPTY = 1,
	TASK_LOADED = 2,
	TASK_LINKED = 3,
	TASK_INITED = 4
}TaskState;

typedef enum OperWay {
	OPERWAY_INIT = 0,
	OPERWAY_AUTO,
	OPERWAY_MANR,
	OPERWAY_MAX
}OperWay;

typedef enum ControlMode
{
	CONTROLMODE_INIT = 0,
	CONTROLMODE_STAND_BY,
	CONTROLMODE_POWER_ON,
	CONTROLMODE_RUN,
	CONTROLMODE_POWER_OFF,
	CONTROLMODE_GUARD_STOP,
	CONTROLMODE_EM_STOP,
	CONTROLMODE_EM_RESET,
	CONTROLMODE_SYS_FAIL,
	CONTROLMODE_UNKNOW
}ControlMode;

typedef enum Language
{
	ENGLISH = 0,
	CHINESE = 1
}Language;

typedef struct MsgPackage
{
	char		msgType[32];  // Event Type
	char     msgDate[64];
	char     msgDec[1024];
	char     msgName[32];  // Event Number
	char     msgTitle[1024];   //Event Title
	unsigned long sequenceNumber;
	int             msgDomain;
	int             msgErrCode;
}MsgPackage;

typedef struct FileInfo
{
	char name[100];
	int isDir;
	char modifyTime[100];
	long size;
}FileInfo;

typedef enum RobotMechUnitType
{
	IRB_NONE = 0,
	GENE_ZJV4,
	IRB_NEZHA,
	IRB_UR_BASE,
	GENE_SCARA,
	IRB_ELBOW_BASE
}RobotMechUnitType;

typedef enum HelperUnitType
{
	NONE_ROBUNIT = 0,         // Mechanical unit type is not defined
	TCPROBOT,               // Mechanical unit is a TCP robot
	MULTIAXES,               // Mechanical unit is a multiple axes manipulator
	SINGLEAXIS              //Mechanical unit is a single unit
}HelperUnitType;

typedef enum RobMechUnitMode
{
	ROBMECHUNITMODE_DEACTIVATED = 0,
	ROBMECHUNITMODE_ACTIVATED
}RobMechUnitMode;

typedef enum
{
	IS_ROBOT = 0,
	IS_AXIS = 1
}MechUnitType;

#define NET_NAME_MAX		250
#define MOTION_MECUNIT_MAX_NUM	8
#define MOTION_GROUP_MAX_NUM	8

typedef struct {
	int id;
	char name[NET_NAME_MAX];
	MechUnitType unitType;
	RobMechUnitMode mode;
	int numOfAxes;
	RobotMechUnitType robotType;
	HelperUnitType tcpType;
}MecUnit;

typedef struct {
	int mecNum;
	MecUnit mecUnits[MOTION_MECUNIT_MAX_NUM];
}RobotControllerInfo;

typedef  enum
{
	INCREMENTAL_MODE_NONE = 0,	// No incremental jogging.
	INCREMENTAL_MODE_SMALL,		// Incremental jogging with small steps.
	INCREMENTAL_MODE_MEDIUM,	    // Incremental jogging with medium sized steps.
	INCREMENTAL_MODE_LARGE,		// Incremental jogging with large steps.
	INCREMENTAL_MODE_USER		// Incremental jogging with user defined step size.
}IncrementalMode;

typedef enum CoordSystem
{
	COORDSYSTEM_UNDEFINED = 0,
	COORDSYSTEM_WORLD,
	COORDSYSTEM_BASE,
	COORDSYSTEM_TOOL,
	COORDSYSTEM_WOBJ
}CoordSystem;

typedef enum HelperJogMode
{
	HELPER_JOG_MODE_UNKNOWN = 0,
	HELPER_JOG_MODE_CARTESIAN = 1,
	HELPER_JOG_MODE_AXISGROUP1,
	HELPER_JOG_MODE_AXISGROUP2,
	HELPER_JOG_MODE_ALIGN,
	HELPER_JOG_MODE_GOTOPOSITION,
	HELPER_JOG_MODE_GOTOJOINT,
	HELPER_JOG_MODE_DRAGTEACH
}HelperJogMode;

#define ROB_ASSIGN_CPP(x, y, fun)  (x=y->fun)
#define ROB_ASSIGN_COM(x, y, fun)  if(y){ x=y->fun; }
#define ROB_FREE_CALLBACK(X)       if(X){ delete X; X= NULL;}
//#define ROB_ASSIGN_ERR(x,y) showErrorMsg(x,y)
#ifdef WIN32
typedef long HRESULT;

#define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
#define FAILED(hr) (((HRESULT)(hr)) < 0)

#define S_ERROR_BSTERP               ((long)-100L) // VAR Pointer is NULL
#define S_ERROR_DESPOIN              ((long)-200L) // RESULT Pointer is NULL
#define S_ERROR_INVALID                ((long)-300L) // VAR VALUE IS NOT VALID
#define S_ERROR_SENDCMD              ((long)-400L) // SETCMD COMMAND IS WRONG
#define S_ERROR_SETTING              ((long)-500L) // The Parameter is WRONG
#define S_ERROR_ALLOCATE             ((long)-600L) // malloc memory is wrong
#define S_ERROR_TIMEOUT              ((long)-700L) // SendCmd Time Out
#else
typedef int HRESULT;

#define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
#define FAILED(hr) (((HRESULT)(hr)) < 0)

#define S_OK 						 ((HRESULT) 0)
#define S_ERROR_BSTERP               ((HRESULT)-100) // VAR Pointer is NULL
#define S_ERROR_DESPOIN              ((HRESULT)-200) // RESULT Pointer is NULL
#define S_ERROR_INVALID                ((HRESULT)-300) // VAR VALUE IS NOT VALID
#define S_ERROR_SENDCMD              ((HRESULT)-400) // SETCMD COMMAND IS WRONG
#define S_ERROR_SETTING              ((HRESULT)-500) // The Parameter is WRONG
#define S_ERROR_ALLOCATE             ((HRESULT)-600) // malloc memory is wrong
#define S_ERROR_TIMEOUT              ((HRESULT)-700) // SendCmd Time Out
#endif

#define SDB_FLAG_SYSTEM_DATA			0x00000001		//system data, can not be deleted
#define SDB_FLAG_SHARED_DATA            0x00000008		//data can not be deleted
#define SDB_FLAG_USER_DATA              0x00000000		//data can not be deleted
#define SDB_FLAG_DEFAULT_DATA              0x00000004

typedef enum DataPropert
{
	UNKNOWN_DATA = -1,
	SYSTEM_DATA = SDB_FLAG_SYSTEM_DATA,
	SHARED_DATA = SDB_FLAG_SHARED_DATA,
	USER_DATA = SDB_FLAG_USER_DATA,
	DEFAULT_DATA = SDB_FLAG_DEFAULT_DATA
}DataPropert;

#define ROB_MOTIONSYSTEM_MECMODE_ACTIVATED				0x0001

#define FTP_FILE_MAX_SIZE	(800*1024*1024)

#define GET_TIMEOUT_NUM  10000
#define set_TIMEOUT_NUM  1000

#define MAX_NUM_OF_JOINTS    7
#define MAX_NUM_OF_EXT_JOINTS   7

#define DATANAME_LENGTH         32

#define AI_TYPE_NAME			"AI"
#define AO_TYPE_NAME			"AO"
#define DI_TYPE_NAME			"DI"
#define DO_TYPE_NAME			"DO"
#define GI_TYPE_NAME			"GI"
#define GO_TYPE_NAME			"GO"
#define TOOL_TYPE_NAME			"TOOLDATA"
#define WOBJ_TYPE_NAME			"WOBJ"
#define LOAD_TYPE_NAME			"LOAD"
#define ZONE_TYPE_NAME			"ZONE"
#define SPEED_TYPE_NAME			"SPEED"
#define TARGET_TYPE_NAME		"ROBTARGET"
#define JOINT_TYPE_NAME			"JOINTTARGET"
#define NUM_TYPE_NAME			"NUM"
#define STRING_TYPE_NAME		"STRING"
#define SEAM_TYPE_NAME			"SEAMDATA"
#define WELD_TYPE_NAME			"WELDDATA"
#define WEAVE_TYPE_NAME			"WEAVEDATA"
#define MULTIPASS_TYPE_NAME		"MULTIPASSDATA"
#define LSCUT_TYPE_NAME			"LSCUTDATA"
#define MULTILAYER_TYPE_NAME	"MULTILAYERDATA"

typedef enum
{
	DAS_DI = 0,
	DAS_DO = 1,
	DAS_AI = 2,
	DAS_AO = 3,
	DAS_TOOLDATA = 4,
	DAS_WOBJ = 5,
	DAS_SPEED = 6,
	DAS_ZONE = 7,
	DAS_ROBOTARGET = 8,
	DAS_LOAD = 9,
	DAS_FRAME = 10,
	DAS_JOINTTARGET = 11,
	DAS_NUM = 12,
	DAS_SEAMDATA = 13,
	DAS_WELDDATA = 14,
	DAS_WEAVEDATA = 15,
	DAS_PRODUCTION_DATA = 16,
	DAS_LSCUTDATA = 17,
	DAS_MULTILAYERDATA = 18,
	DAS_MULTIPASSDATA = 19,
	DAS_GI = 20,
	DAS_GO = 21,
	DAS_UNKNOWN = 100
}DasDataType;

typedef enum
{
	BUS_VIRTUAL = 0,
	BUS_ECAT = 1,
	DEVICE_NET = 2,
	PROFI_NET = 3,
	BUS_SAFETY = 4,
	BUS_MODBUS = 5,
	BUS_SYSTEM = 6,
	BUS_CAN = 7,
	BUS_ECATGATEWAY = 8,
	BUS_ETHERNETIP = 9,
	BUS_ECATSERVO = 10,   // EtherCAT servo
	BUS_UARTIO = 11,
	BUS_SAFIO = 12
}BusType;

typedef struct
{
	char name[DATANAME_LENGTH + 1];
	DasDataType ioType;
	BusType busType;
	int unitNo;
	int unitMap;
	int dataLen;
	int defaultValue;
	int value;
	int bActived;			/*1 is actived; 0 is deactived.*/
	int bSimulation;		/*1 is simulation; 0 is unsimlation.*/
	char connect[DATANAME_LENGTH + 1]; //name for connected signal
}IoItem;

typedef struct Cartesian
{
	float x;
	float y;
	float z;
}Cartesian;

typedef struct Quaternion
{
	float u0, u1, u2, u3;
}Quaternion;

typedef struct
{
	Cartesian pos;
	Quaternion orient;
}Pose;

typedef struct
{
	float inertia[6];               /* {Ixx, Iyy, Izz, Ixy, Iyz, Izx} */
} CentralInertia;

typedef enum
{
	CUTIN_NO = 0,
	CUTIN_CIRC = 1,
	CUTIN_LINE = 2,
	CUTIN_MAX
}CutinType;

typedef enum
{
	CUTOUT_NO = 0,
	CUTOUT_CIRC = 1,
	CUTOUT_LINE = 2,
	CUTOUT_MAX
}CutoutType;

typedef enum
{
	CUT_IN = 1,
	CUT_OUT = 2,
	CUT_MAX
}CutType;

typedef struct
{
	float circleDiam;
	float sideLength1;
	float sideLength2;
	float cutSpeed;
	float laserPower;
	float dutyCycle;
	int   laserOnDelay;
	int   laserOffDelay;
	float gasFlow;
	CutinType   cutInType;
	float cutInScale;
	CutoutType   cutOutType;
	float cutOutScale;
	CutType   cutType;
	float kerfComp;
	int   stopTime;
	float followHeight;
	float followSensit;
}LsCutDaTa;

typedef struct
{
	int     lsYMoveNum;
	int     lsYLenSel;//it is BOOL
	float   lsYMoveDistance;
	int     lsZMoveNum;
	float   lsZMoveDistance;
	float   lsZPowerDecScale;
	int     lsYSuspendNum;
	float   lsYSuspendDeviation;
	int     lsYSuspendTime;
	int     lsZSuspendNum;
	int     lsZSuspendTime;
	int     lsXArrNum;
	float   lsXArrDistance;
	int     lsYArrNum;
	float   lsYArrDistance;
	float   lsPowerScale;
}MultiLayerData;

typedef struct
{
	int  sched;		// program number
	int  mode;		// welding mode;
	float volt;		// using arc ignition , the value of the voltage. default value is 100  unit : 0.1V(%)
	float wirefeed;  // using arc ignition , the value of wire feed speed. default value is 10 unit: mm/s
	float current;   // using arc ignition , the value of electric current. default value is 100 unit: A
}ArcData;

typedef struct
{
	float purgeTime;		// the time of welding machine gasing
	float preflowTime;		// the time of gasing before welding machine lunch
	ArcData ignArcData;
	float ignMoveDelay;	 //delay time after arc weld succeed.default value is 100 unit: ms
	float scrapeStart;      //book param , no use at current
	float heatSpeed;		//the speed of weld heating. default value is 100 unit: mm/s
	float heatTime;			//the time of weld heating. default value is 100 unit: ms
	float heatDistance;     //the length of weld heating. default value is 10 unit: mm  if > 0 heat_time is invalid
	ArcData heatArcData;
	float coolTime;			//cool time for weld end. default value is 100, unit: ms
	float fillTime;			//no use at current
	ArcData fillArcData;
	float bbackTime;
	float rbackTime;
	ArcData bbackArcData;
	float postflowTime;		//the time after welding ended.
}SeamData;

typedef struct
{
	float weldSpeed;          // weld speed default value is 100 unit: mm/s
	ArcData mainArc;
}WeldData;

typedef enum
{
	NO_WEAVE = 0,
	WEAVE_XY_Z,
	WEAVE_YZ_V,
	WEAVE_YZ_TRI,
	WEAVE_XY_CIR,
	WEAVE_XY_SIN,
	WEAVE_UNKNOW
}WeaveShape;

typedef enum
{
	WEAVE_ARM = 0,
	WEAVE_WRIST,
	WEAVE_123,
	WEAVE_456,
	WEAVE_NUKOWN
}WeaveType;

typedef struct
{
	WeaveShape weaveShape;
	WeaveType weaveType;
	float weaveLength;              //unit mm
	float weaveWidth;               //unit mm
	float weaveHeight;              //unit mm
	float dwellLeft;				//unit mm
	float dwellCenter;             //unit mm
	float dwellRight;			  //unit mm
	float weaveDir;
	float weaveTilt;
	float weaveOri;
	float weaveBias;
}WeaveData;

typedef struct
{
	int direction;
	float approachDistance;  //起点高度
	float departDistance;    //结束点高度
	float startOffset;
	float endOffset;
	float seamOffsY;
	float seamOffsZ;
	float seamRotX;
	float seamRotY;
	float seamRotZ;
}MultipassData;

typedef struct
{
	float mass;
	Pose pose;                            /* Center of mass coordinate system */
	CentralInertia centralInertia;
} LoadData;

typedef  struct
{
	int robHold;
	Pose toolFrame;
	LoadData load;
	//	char tool_name[SYS_MAX_SIZE_OF_SDB_URL + 1];
}ToolData;

typedef  struct
{
	int  robHold;
	int  ufprog;
	char ufmecName[DATANAME_LENGTH + 1];
	Pose uframe;
	Pose oframe;
} WobjData;

typedef struct
{
	int   finep;                //finep need to config: TRUE->FINEPOINT;FALSE->ZONE
	float tcpM;               //need to config;
	float oriTcpM;           //need to config;
							   /*****inside set ****/
	float extTcpM;
	float oriRad;
	float extM;
	float extRad;
}ZoneData;

typedef struct
{
	float tcpMS;
	float oriRadS;
	float extMS;
	float extRadS;
}SpeedData;

typedef struct
{
	float num;
}NumData;

typedef struct
{
	char val[250];
}StringData;

typedef struct RobTargetConfig
{
	int quarterRevJ1;
	int quarterRevJ4;
	int quarterRevJ6;
	int quarterRevJx;
}RobTargetConfig;

typedef struct HelperRobTarget
{
	Cartesian pos;                       //app unit : mm     kernel unit :m
	Quaternion orient;
	RobTargetConfig config;          //quarter_rev_j1,quarter_rev_j4,quarter_rev_j6,quarter_rev_jx
	float extJoints[MAX_NUM_OF_EXT_JOINTS];  //6   this var is 6 when running 7dof ; is 12 when running 6dof;
	float armAngle;                 //这两项组合起来就是Pose
}HelperRobTarget;

typedef struct HelperRobJointTarget
{
	float rax[MAX_NUM_OF_JOINTS];
	float eax[MAX_NUM_OF_EXT_JOINTS];
}HelperRobJointTarget;

typedef struct
{
	int element[7];    //0~2048
}RobJogVector;

typedef void(*CallbackFindIp)(const char *ip);
typedef void(*CallbackLoseIp)(const char *ip);

typedef void(*CallbackIOChanged)(void *handle, DasDataType type, const char *name, int value, int sim);
typedef void(*CallbackEventChanged)(void *handle, unsigned long seqno);

typedef struct {
	void *rc;
	char ip[NET_NAME_MAX];
	RobotControllerInfo info;

	void *interSubcribe;
}RobotControllerHandle;

#endif // !ROBOTCOMMON
